﻿using AutoMapper;
using ProjectManageSystemApi.DbEntity;
using ProjectManageSystemApi.Enums;
using ProjectManageSystemApi.Models;
using System;
using System.Collections.Generic;

namespace ProjectManageSystemApi.Services.Impl
{
    public class SupplierService : ISupplierService
    {
        readonly IFreeSql _fsql;
        readonly IMapper _mapper;
        public SupplierService(IFreeSql fsql, IMapper mapper)
        {
            _fsql = fsql;
            _mapper = mapper;
        }

        /// <summary>
        /// true 创建成功
        /// </summary>
        /// <param name="supplierInfo"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool Create(SupplierInfoDto supplierInfo, long userId)
        {
            var info = _mapper.Map<SupplierInfo>(supplierInfo);
            info.CreateTime =DateTime.Now;
            info.State = (sbyte)EnumSupplierInfo.Nomal;
            info.CreateUser = userId;
            return _fsql.Insert<SupplierInfo>(info).ExecuteIdentity() > 0;
        }

        public bool Update(SupplierInfoDto supplierInfo)
        {
            var data = _mapper.Map<SupplierInfo>(supplierInfo);
            return _fsql.Update<SupplierInfo>()
                .SetSource(data)
                .UpdateColumns(u => new { u.SupplierName, u.SupplierAddress, u.ContactsUser,u.ContactsPhone })
                .ExecuteAffrows() > 0;
        }

        public bool Delete(int id)
        {
            return _fsql.Update<SupplierInfo>(id)
                .Set(x=>x.State, (sbyte)EnumSupplierInfo.Delete)
                .ExecuteAffrows() > 0;
        }

        public bool ExistById(int id)
        {
            return _fsql.Select<SupplierInfo>().Any(p => p.Id == id && p.State == (sbyte)EnumSupplierInfo.Nomal);
        }

        /// <summary>
        /// true 已存在 false不存在
        /// </summary>
        /// <param name="supplierName"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool ExistBySupplierName(string supplierName, int id = 0)
        {
            return _fsql.Select<SupplierInfo>().Any(p => p.Id != id && p.SupplierName == supplierName && p.State == (sbyte)EnumSupplierInfo.Nomal);
        }

        public Models.PageVo<IEnumerable<SupplierInfoDto>> GetAll(Models.PageBo<SupplierParams> pageBo)
        {
            var datas = _fsql.Select<SupplierInfo>()
                .Include(x=>x.UserInfo)
                .Where(p => p.State == (sbyte)EnumSupplierInfo.Nomal);

            if (!string.IsNullOrWhiteSpace(pageBo.Param.SupplierName))
            {
                datas = datas.Where(d => d.SupplierName.Contains(pageBo.Param.SupplierName));
            }
            if (!string.IsNullOrWhiteSpace(pageBo.Param.ContactsUser))
            {
                datas = datas.Where(d => d.ContactsUser.Contains(pageBo.Param.ContactsUser));
            }
            var total = datas.Count();
            datas = datas.Page(pageBo.Page, pageBo.Size);

            var data = new Models.PageVo<IEnumerable<SupplierInfoDto>>
            {
                Page = pageBo.Page,
                Size = pageBo.Size,
                Datas = _mapper.Map<List<SupplierInfoDto>>(datas.ToList()),
                Total = total
            };
            return data;
        }
    }
}
